<template>
  <div class="sent-message-box">
    <div class="message-content">
      <VirtualList
        style="height: 100%; overflow: auto"
        ref="vsl"
        :data-key="'id'"
        :data-sources="messageList"
        :data-component="messageItem"
        :extra-props="{ userId }"
        @scroll="onScroll"
        @totop="onToTop"
        @tobottom="onBottom"
        :keeps="200"
      >
        <template #header>
          <div v-show="loading" class="header">
            <Loading />
          </div>
        </template>
      </VirtualList>

      <Rules :gameType="currentGame" ref="rules" />

      <div v-if="showToBottomBtn" class="to-bottom" @click="clickToBottom">
        <span v-if="unreadCount > 0" class="count">{{ unreadCount > 999 ? '999+' : unreadCount }}</span>
        <Icon class="arraw" name="arrow-down" />
      </div>
    </div>

    <div class="bottom">
      <img v-show="isShowBetTip" src="@/assets/img/chatroom/bet_tip.png" @click="handleShowPopup" alt="" />
      <input
        class="chat-input"
        type="text"
        v-model="inputValue"
        placeholder="请输入..."
        @keydown.enter="enter"
        @focus="onFocus"
        @blur="onBlur"
      />
      <div class="speak-rg" @click="handleBtn" :class="{ isFengpan }">
        <div class="all-center">
          {{ !!inputValue || isInput ? '发送' : '下注' }}
        </div>
      </div>
    </div>

    <template v-if="showCancelOrder">
      <investPopup :chatroomCancelId="cancelId" @closeCancelOrder="closeCancelOrder" ref="invest" />
    </template>
  </div>
</template>

<script>
import { defineAsyncComponent } from 'vue'
import { computed } from 'vue'
import messageItem from './messageItem.vue'
// import VirtualList from 'vue3-virtual-scroll-list'
import { useGameStore } from '@/stores/game'
import { useUserStore } from '@/stores/user'
// import investPopup from '@/components/invest-popup.vue'
// import Rules from './rules.vue'
import chatroom from './chatroom'
import { GAME_STATUS_CODE } from '@/enum'
import { showToast, Loading, Icon } from 'vant'

const investPopup = defineAsyncComponent(() => import('@/components/invest-popup.vue'))
const Rules = defineAsyncComponent(() => import('./rules.vue'))
const VirtualList = defineAsyncComponent(() => import('vue3-virtual-scroll-list'))

export default {
  name: 'chatroom',

  components: { VirtualList, investPopup, Rules, Loading, Icon },

  mixins: [chatroom],

  props: ['gameType'],

  data() {
    return {
      inputValue: '',
      isShowBetTip: false,
      showCancelOrder: false,
      cancelId: null,
      isInput: false,
    }
  },

  setup() {
    const userStore = useUserStore()

    const userId = computed(() => userStore.userInfo?.id)

    const token = computed(() => userStore.token)

    const gameStore = useGameStore()

    const currentGame = computed(() => gameStore.currentGame)

    const isFengpan = computed(
      () =>
        gameStore?.drawingStatus?.code === GAME_STATUS_CODE.fengpan.code ||
        gameStore?.drawingStatus?.code === GAME_STATUS_CODE.fengpanAjax.code,
    )

    return { userId, token, currentGame, messageItem, isFengpan }
  },

  methods: {
    handleBtn() {
      if (this.isFengpan) {
        showToast('封盘时间不能聊天和下注~请稍后再试')
        return
      }

      if (this.isInput || !!this.inputValue) {
        this.enter()
      } else {
        this.$emit('bet')
      }
    },

    onFocus() {
      this.isInput = true
    },

    onBlur() {
      setTimeout(() => {
        this.isInput = false
      }, 300)
    },

    enter() {
      if (this.isFengpan) {
        showToast('封盘时间不能聊天和下注~请稍后再试')
        return
      }

      this.sendMessage()
    },

    closeCancelOrder() {
      this.showCancelOrder = false
    },
  },
}
</script>

<style lang="less" scoped>
.sent-message-box {
  height: 100%;
  background: url('@/assets/img/chatroom/msg_bg.png') center center no-repeat;
  background-size: cover;

  .message-content {
    height: calc(100% - 56px);
    padding: 0 12px;
    transform: translate(0);
  }

  .bottom {
    position: relative;
    display: flex;
    align-items: center;
    height: 56px;
    width: 100%;
    padding: 12px 12px 12px 9px;
    box-sizing: border-box;
    border-radius: 8px 8px 0 0;
    box-shadow: 0px -2px 4px 0px rgba(0, 0, 0, 0.05);
    background: rgba(30, 31, 49, 1);

    .speak-rg {
      width: 60px;
      height: 30px;
      font-size: 14px;
      font-weight: 600;
      background: linear-gradient(90deg, #7144f0 2.6%, #4e26c0 21.88%, #9e70ff 49.52%, #531fc2 82.81%, #7144f0 100%);
      box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
      border-radius: 4px;

      > div {
        width: 100%;
        height: calc(100% - 1px);
        border-radius: 4px;
        background: linear-gradient(21.5deg, #3d35c6 6.96%, #6c4fe0 88.93%);
      }

      &.isFengpan {
        > div {
          background: radial-gradient(113.17% 113.81% at 94.17% 0%, #b7c2cf 0%, #7f92a9 100%);
        }
      }
    }

    .chat-input {
      height: 32px;
      padding: 0 6px;
      flex: 1;
      border-radius: 4px;
      background: rgba(47, 49, 75, 1);
      margin-right: 8px;
      border: none;
      color: #fff;
      font-size: 12px;
    }
  }

  img {
    display: block;
    width: 22px;
    height: 22px;
    margin-right: 6px;
  }
}

.to-bottom {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 28px;
  height: 28px;
  bottom: 20px;
  right: 20px;
  border-radius: 50%;
  color: #fff;
  background: linear-gradient(270deg, #6c4fe0 0%, #3d35c6 100%);

  .arraw {
    font-size: 14px;
    font-weight: 600;
  }

  .count {
    position: absolute;
    left: 50%;
    top: -2px;
    transform: translate(-50%, -100%);
    padding: 1px 3px;
    font-size: 12px;
    background: linear-gradient(
      180deg,
      rgba(163, 143, 255, 0.55) 0%,
      rgba(120, 97, 247, 0.55) 46.88%,
      rgba(195, 189, 255, 0.55) 100%
    );
    border-radius: 8px;
    line-height: 1;
  }
}
</style>
